package dao;

import entry.DiscPaiHang;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Set;
import java.util.TreeSet;

public class LeaderboardDao {
    public static Set<DiscPaiHang> leaderboardDao() {
        Set<DiscPaiHang> set = new TreeSet<>((o1, o2) -> o2.getKet()- o1.getKet());
        PreparedStatement preparedStatement = null;
        Connection connection = null;
        ResultSet resultSet = null;
        try {
            connection = DBConnection.getConnection();
            preparedStatement = connection.prepareStatement("select d_name,d_key from discs ");
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                set.add(new DiscPaiHang(resultSet.getString(1), resultSet.getInt(2)));
            }
            return set;
        } catch (Exception e) {
            return null;
        } finally {
            DBConnection.release(connection, preparedStatement, resultSet);
        }
    }
}
